package com.gwm.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwm.common.enums.ExceptionEnums;
import com.gwm.common.exceptions.LyException;
import com.gwm.common.utils.BeanHelper;
import com.gwm.item.dto.SpecParamDTO;

import com.gwm.item.service.TbSpecParamService;
import com.gwm.item.mapper.TbSpecParamMapper;
import com.gwm.item.pojo.TbSpecParam;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 规格参数组下的参数名 服务实现类
 * </p>
 *
 * @author heima
 * @since 2020-12-01
 */
@Service
public class TbSpecParamServiceImpl extends ServiceImpl<TbSpecParamMapper, TbSpecParam> implements TbSpecParamService {

    @Override
    public List<SpecParamDTO> findSpecParamByCategoryIdOrGroupId(Long categoryId, Long groupId, Boolean searching) {
//        有可能根据分类id，也有可能根据组id，但是这两个必须要有其中一个

        if(categoryId==null&&groupId==null){
            throw new LyException(ExceptionEnums.INVALID_PARAM_ERROR);
        }


        QueryWrapper<TbSpecParam> queryWrapper = new QueryWrapper<>();

        if(categoryId!=null){
            queryWrapper.lambda().eq(TbSpecParam::getCid,categoryId);
        }
        if(groupId!=null){
            queryWrapper.lambda().eq(TbSpecParam::getGroupId,groupId);
        }
        if(searching!=null){
            queryWrapper.lambda().eq(TbSpecParam::getSearching,searching);
        }

        List<TbSpecParam> tbSpecParamList = this.list(queryWrapper);

        if(CollectionUtils.isEmpty(tbSpecParamList)){
            throw new LyException(ExceptionEnums.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(tbSpecParamList,SpecParamDTO.class);
    }
}
